package com.osmino.contacts.model.contacts;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ContactAndroid implements IContact{
	
	private HashMap<String, Object> values;
	
//	public static int EQUALS_METHOD = 0;
//	public static final int EQUALS_BY_OBJ = 0;
//	public static final int EQUALS_BY_ID = 1;
//	public static final int EQUALS_BY_RAW_ID = 2;
	
	public ContactAndroid(){
		values = new HashMap<String, Object>();
	}
	
	@Override
	public IContactBuilder getBuilder() {
		if(isEmpty()){
			return new ContactBuilderAndroid();
		}
		else{
			return new ContactBuilderAndroid(this);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getData(String which) {
			return (T)values.get(which);
	}
	
	public <T> T getData(String which, Class<T> type) {
			return type.cast( values.get(which) );
	}
	
	public Set<String> getKeys(){
		return values.keySet();
	}
	
	public boolean containsKey(String key){
		return values.containsKey(key);
	}
	
	public int size(){
		return values.size();
	}
	
	public boolean isEmpty(){
		return values.isEmpty();
	}

	@Override
	public void setData(String which, Object object) {
		values.put(which, object);
	}
	
	@Override
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("<Contact>\n");
		for(Map.Entry<String, Object> entry : values.entrySet()){
			sb.append("    ");
			sb.append( entry.getKey() );
			sb.append( "=>" );
			sb.append( entry.getValue().toString() );
			sb.append( "\n" );
		}
		sb.append("</Contact>\n");
		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((values == null) ? 0 : values.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ContactAndroid other = (ContactAndroid) obj;
		if (values == null) {
			if (other.values != null)
				return false;
		} else if (!values.equals(other.values))
			return false;
		return true;
	}	
}